import 'package:boss/common/common.dart';
import 'package:boss/models/sku.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusCommodityByFormField/page/skuSetting/provider/sku_provider.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusCommodityByFormField/widget/scan_icon.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:boss/ui/widget/tab_btn.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class SkuCard extends StatefulWidget {
  final Sku data;
  final int index;

  const SkuCard({
    Key key,
    @required this.data,
    this.index,
  }) : super(key: key);

  @override
  _SkuCardState createState() => _SkuCardState();
}

class _SkuCardState extends State<SkuCard> {
  TextEditingController _skuCodeController;

  @override
  void initState() {
    super.initState();

    _skuCodeController = TextEditingController(text: widget.data.commoditySkuCode);
  }

  @override
  void dispose() {
    _skuCodeController.dispose();
    _skuCodeController = null;

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(
              top: 20.w,
              bottom: 50.w,
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                SizedBox(
                  width: 30.w,
                ),
                TabBtn(
                  title: '规格${widget.index + 1}',
                  isSelected: true,
                ),
                GestureDetector(
                  onTap: () {
                    var skuProvider = Provider.of<SkuProvider>(
                      context,
                      listen: false,
                    );
                    skuProvider.remove(widget.data);
                  },
                  child: BaseImage(
                    'assets/image/delete_icon.png',
                    width: 30.w,
                    height: 30.w,
                  ),
                ),
              ],
            ),
          ),
          BaseTextFormField(
            prefixText: '规格名称',
            isRequired: true,
            hintText: '例如：大份、小份',
            initialValue: widget.data.skuName,
            maxLength: 10,
            validator: (value) {
              if (value == '') {
                return '规格名称不能为空';
              }

              return null;
            },
            onChanged: (value) {
              widget.data.skuName = value;
            },
            actions: ['大份', '中份', '小份', '大杯', '中杯', '小杯'],
          ),
          if (Constant.isTakeout)
            BaseTextFormField(
              prefixText: '外卖售价',
              isRequired: true,
              hintText: '请输入',
              suffixText: '元',
              keyboardType: TextInputType.numberWithOptions(decimal: true),
              inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.numberReg)],
              initialValue: AmountUtil.price2String(widget.data.skuPrice),
              maxLength: 10,
              validator: (value) {
                if (value == '') {
                  return '外卖售价不能为空';
                }

                return null;
              },
              onChanged: (value) {
                try {
                  widget.data.skuPrice = AmountUtil.string2Price(value);
                } catch (e) {
                  widget.data.skuPrice = null;
                }
              },
            ),
          BaseTextFormField(
            prefixText: Constant.isTakeout ? '外卖原价' : '售卖价',
            isRequired: !Constant.isTakeout,
            hintText: '请输入',
            suffixText: '元',
            keyboardType: TextInputType.numberWithOptions(decimal: true),
            inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.numberReg)],
            initialValue: AmountUtil.price2String(Constant.isTakeout ? widget.data.skuOriginalPrice : widget.data.skuPrice),
            maxLength: 10,
            validator: (value) {
              if (value == '' && !Constant.isTakeout) {
                return Constant.isTakeout ? '外卖原价不能为空' : '售卖价不能为空';
              }

              return null;
            },
            onChanged: (value) {
              if (Constant.isTakeout) {
                try {
                  widget.data.skuOriginalPrice = AmountUtil.string2Price(value);
                } catch (e) {
                  widget.data.skuOriginalPrice = null;
                }
              } else {
                try {
                  widget.data.skuPrice = AmountUtil.string2Price(value);
                } catch (e) {
                  widget.data.skuPrice = null;
                }
              }
            },
          ),
          if (!Constant.isTakeout)
            BaseTextFormField(
              prefixText: '会员价',
              hintText: '请输入',
              suffixText: '元',
              keyboardType: TextInputType.numberWithOptions(decimal: true),
              inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.numberReg)],
              initialValue: AmountUtil.price2String(widget.data.skuMemberPrice),
              maxLength: 10,
              onChanged: (value) {
                try {
                  widget.data.skuMemberPrice = AmountUtil.string2Price(value);
                } catch (e) {
                  widget.data.skuMemberPrice = null;
                }
              },
            ),
          BaseTextFormField(
            controller: _skuCodeController,
            prefixText: '商品条形码',
            hintText: '可填写条形码或扫码',
            maxLength: 50,
            suffix: ScanIcon(
              onScan: (code) {
                widget.data.commoditySkuCode = code;
                _skuCodeController.text = code;
              },
            ),
            onChanged: (value) {
              widget.data.commoditySkuCode = value;
            },
          ),
        ],
      ),
    );
  }
}
